﻿using System;
using System.Drawing;
using System.Windows.Forms;

using DBF_ReadWrite;

namespace OpenShape
{
    public partial class Toc_Layer : UserControl
    {

        public event Deligate_VisibleChanged Toc_Visble;  // 델리게이트
        public event Deligate_LayerColorChanged Toc_LayerColorChanged;  // 델리게이트
        public event Deligate_Form_ID TOC_layerfullextent;  // 델리게이트
        public event Deligate_Form_ID TOC_isLayerSelected;  // 델리게이트
        public event Deligate_Form_None Toc_Refresh;  // 델리게이트
        public event Deligate_Form_ID Toc_LayerRemove;    // 델리게이트
        public event Deligate_LabelShow Toc_LabelShow; // 델리게이트
        public event Deligate_ChangedLabelsProp TOC_ChangedLabelsProp; // 델리게이트

        public string TOC_DataSourceName;
        public bool isLSelected = false;
        public int shape_type;
        public bool isVisible = true;
        public bool isExtend = true;
        public int isID;

        public float penThickness; //펜두께 (선두께)
        public Color penColor;  //펜색상 (선색상)
        public Color brushColor;  // 브러쉬 컬러 (면색상)

        public Font fonts = new Font(   //라벨폰트
        new FontFamily("Arial"),
        10,
        FontStyle.Regular,
        GraphicsUnit.Point);
        public Color pFontColor = Color.Black;  //라벨폰트 색상
        public string pFieldName;

        DBF_FileReadWriter DBf_FileRW = new DBF_FileReadWriter();


        public Toc_Layer()
        {
            InitializeComponent();
        }

        private void l_Name_Edit_TextChanged(object sender, EventArgs e)
        {
            l_Name.Text = l_Name_Edit.Text;
            l_Name.Visible = true;
            l_Name_Edit.Visible = false;
        }

        private void l_Name_Edit_KeyPress(object sender, KeyPressEventArgs e)
        {
            l_Name.Text = l_Name_Edit.Text;
            l_Name.Visible = true;
            l_Name_Edit.Visible = false;
        }
        private void l_Name_DoubleClick(object sender, EventArgs e)
        {
            l_Name_Edit.Text = l_Name.Text;
            l_Name.Visible = false;
            l_Name_Edit.Visible = true;
        }

        private void l_type_color_Paint(object sender, PaintEventArgs e)
        {
            Graphics g2 = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(this.brushColor);
            Pen pPen = new Pen(this.penColor, this.penThickness);

            if (shape_type == 1)
            {
                g2.FillEllipse(blueBrush, l_type_color.Width / 2 - 2, l_type_color.Height /2 -2, 4, 4);
                g2.DrawEllipse(pPen, l_type_color.Width / 2 - 2, l_type_color.Height / 2 - 2, 4, 4);
            }

            if (shape_type == 3)
            {
                g2.DrawLine(pPen, 0, l_type_color.Height / 2, l_type_color.Width, l_type_color.Height / 2);
            }
            if (shape_type == 5)
            {
                g2.FillRectangle(blueBrush, 1, 1, l_type_color.Width-2, l_type_color.Height-2);
                g2.DrawRectangle(pPen, 1, 1, l_type_color.Width-2, l_type_color.Height-2);
            }
        }

        private void l_Visible_CheckedChanged(object sender, EventArgs e)
        {
            if (l_Visible.Checked == true)
            {
                isVisible = true;
                Toc_Visble(true, isID );
            }
            else
            {
                isVisible = false; 
                Toc_Visble(false,isID);
            }
        }

        private void l_Name_MouseClick(object sender, MouseEventArgs e)
        {
            isLayerSelected();
            if (e.Button == MouseButtons.Right)
            {
                Toc_Menu.Show(this, e.X, e.Y);
            }

        }

        private void l_type_color_Click(object sender, EventArgs e)
        {
            LayerColor diagLayerColor = new LayerColor(penColor, brushColor, penThickness, shape_type, isID);
            diagLayerColor.LayerColor_LayerColorChanged += new Deligate_LayerColorChanged(LayerColor);
          
            diagLayerColor.Show();
        }

        private void LayerColor(Color lc_penColor, Color lc_brushColor, float lc_penThickness, int id)
        {
            penColor = lc_penColor;
            brushColor = lc_brushColor;
            penThickness = lc_penThickness;
            Toc_LayerColorChanged(penColor, brushColor, penThickness, id);
            l_type_color.Invalidate();
        }

        private void menuLayerExtent_Click(object sender, EventArgs e)
        {
            TOC_layerfullextent(isID);
        }

        private void isLayerSelected()
        {
            TOC_isLayerSelected(isID);
        }

        private void Toc_Layer_Click(object sender, EventArgs e)
        {
            isLayerSelected();
        }

        private void l_Name_Click(object sender, EventArgs e)
        {
            isLayerSelected();
        }

        private void l_Extend_Click(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                isExtend = false;
                l_Extend.ImageIndex = 1;
                this.Height = 17;
            }
            else
            {
                isExtend = true;
                l_Extend.ImageIndex = 0;
                this.Height = l_type_color.Top + l_type_color.Height + 2;
            }
            Toc_Refresh();
        }

        private void menuLayerRemove_Click(object sender, EventArgs e)
        {
            Toc_LayerRemove(isID);
        }

        private void menuShowTable_Click(object sender, EventArgs e)
        {
            LayerManager.m_Layers[isID].m_DBF = DBf_FileRW.Reader(LayerManager.m_Layers[isID].m_DataSourceName);
            TableView diagTableView = new TableView(isID, TOC_DataSourceName);
            diagTableView.Show();
        }

        private void menuLabelShow_Click(object sender, EventArgs e)
        {
            if (menuLabelShow.Checked == false)
            {
                menuLabelShow.Checked = true;
                Toc_LabelShow(true, isID);
            }
            else
            {
                menuLabelShow.Checked = false;
                Toc_LabelShow(false, isID);
            }
        }

        private void menuProperties_Click(object sender, EventArgs e)
        {
            Prop diagProp = new Prop(fonts, pFontColor, menuLabelShow.Checked, isID, pFieldName);
            diagProp.prop_ChangedLabelsProp += new Deligate_ChangedLabelsProp(ChangedLabelsProp);
            LayerManager.m_Layers[isID].m_DBF = DBf_FileRW.Reader(LayerManager.m_Layers[isID].m_DataSourceName);
            diagProp.Show();
        }

        private void ChangedLabelsProp(Font p_fonts, Color p_FontColor, bool p_showLabelChk, int ID, string p_fieldName)
        {
            fonts = p_fonts;
            menuLabelShow.Checked = p_showLabelChk;
            pFontColor = p_FontColor;
            pFieldName = p_fieldName;

            TOC_ChangedLabelsProp(fonts, pFontColor, menuLabelShow.Checked, ID, p_fieldName);
        }

    }
}